package com.accp.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/upload")
public class UploadController {

	@RequestMapping("/uploadjson")
	public void uploadImg(@RequestParam("imgFile") MultipartFile file,HttpServletRequest request,HttpServletResponse response){
		//文件保存目录路径
		String savePath = request.getServletContext().getRealPath("/") + "upload/";
		HashMap<String, String> data = new HashMap<>();
		//文件保存目录URL
		String saveUrl  = request.getContextPath() + "/upload/";
		String fileName = file.getOriginalFilename();
        //String filePath = "/Users/itinypocket/workspace/temp/";
        File dest = new File(savePath + fileName);
        JSONObject obj = new JSONObject();	
        try {
            file.transferTo(dest);
            obj.put("error", 0);
    		obj.put("url", saveUrl + fileName);

        } catch (IOException e) {
        	obj.put("error", "0");
        	obj.put("message", "上传失败");
        }
        try {
			response.getWriter().print(obj.toJSONString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
